package com.gxa.modules.community.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.community.entity.*;
import com.gxa.modules.community.service.CarManageService;
import com.gxa.modules.community.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "车辆管理")
public class CarManageController {
    @Autowired
    private CarManageService carManageService;
    @ApiOperation("车辆管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "username",value ="车位状态",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "carnum",value ="车位编号",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "serialNo",value ="人的身份id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value ="小区id",dataType ="int"),
    }
    )
    @GetMapping("/car/list")
    public Result<List<Car>> list01(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<Car>> r=Result.success();
        String page1=(String) params.get("page");
        String limit1=(String)params.get("limit");
        Integer page=Integer.valueOf(page1);
        Integer limit=Integer.valueOf(limit1);
        try {
            PageHelper.startPage(page,limit);
            List<Car> cars = this.carManageService.queryByPage(params);
            PageInfo<Car> pageInfo = new PageInfo<>(cars);
            long total = (int) pageInfo.getTotal();
            r=Result.success(cars,total);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车辆品牌下拉查询")
    @GetMapping("/cartype/list")
    public Result<List<CarType>> list02(){
        Result<List<CarType>> r=Result.success();
        try {
            List<CarType> carTypes = this.carManageService.queryCarType();
            r=Result.success(carTypes);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("车辆管理 添加")
    @PostMapping("/car/add")
    public Result<List<CarAU>> carManageAdd(@RequestBody CarAU carAU){

        System.out.println(carAU);
        Result<List<CarAU>> r=Result.success();
        try {
            this.carManageService.carManageAdd(carAU);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车辆管理 修改")
    @PutMapping("/car/update")
    public Result<List<CarAU>> carManageUpdate(@RequestBody CarAU carAU){
        System.out.println(carAU);
        Result<List<CarAU>> r=Result.success();
        try {
            this.carManageService.carManageUpdate(carAU);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车辆管理 删除")
    @DeleteMapping("/car/delete")
    public Result<List<SpaceE>> carManageDelete(Integer id){
        Result<List<SpaceE>> r=Result.success();
        try {
            this.carManageService.carManageDelete(id);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车辆管理 批量删除")
    @DeleteMapping("/car/ids/delete")
    public Result<List<SpaceE>> carManageDeleteIds(@RequestBody List<Integer> ids){
        Result<List<SpaceE>> r=Result.success();
        try {
            this.carManageService.carManageDeleteIds(ids);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

}
